package com.example.blog.controller;


import com.example.blog.common.pojo.Result;
import com.example.blog.common.pojo.request.AddBlogParam;
import com.example.blog.common.pojo.request.UpBlogParam;
import com.example.blog.common.pojo.response.BlogInfoResponse;
import com.example.blog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {

    //Resource里面是Bean的名称
    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    //    @Autowired
//    private BlogService blogServiceImpl;
    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList() {

        log.info("获取博客列表");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfoResponse getBlogDetail(@NotNull Integer blogId) {
        log.info("获取博客详情,blog.id:{}", blogId);
        return blogService.getBlogDetail(blogId);
    }

    @RequestMapping("/add")
    //@PostMapping("/add")
    //@putMapping("/add")
    //如果两个或者三个参数,那就把他变成json,@Validated用来校验
    public Boolean addBlog(@Validated @RequestBody AddBlogParam param) {
        log.info("添加博客,标题:{}", param.getTitle());
        return blogService.addBlog(param);
    }

    @RequestMapping("/update")
    public Boolean updateBlog(@Validated @RequestBody UpBlogParam upBlogParam) {
        log.info("删除博客,博客ID:{}", upBlogParam.getId());
        return blogService.updateBlog(upBlogParam);
    }

    @RequestMapping("/delete")
    public Boolean deleteBlog(@NotNull Integer blogId) {
        log.info("更新博客,博客ID:{}", blogId);
        return blogService.deleteBlog(blogId);
    }
}
